<template>
	<div class="components-container">
        <div class="header-form">
            <div class="layui-inline">
              <label class="layui-form-label" >统计条件</label>
              <div class="layui-input-inline">
                  <el-cascader :options="options" :show-all-levels="true" filterable v-model="selectData.options" change-on-select></el-cascader>
              </div>
            </div>
            <div class="layui-inline">
                <label>火化日期从</label>
                <el-date-picker v-date v-model="selectData.RegdateGreaterAndEquals" type="date" placeholder="输入日期"/>
                 到 <el-date-picker v-date v-model="selectData.RegdateLessAndEquals" type="date" placeholder="输入日期"/>
            </div>
            <el-button type="primary" @click="btnSearch" :disabled="downloadLoading" :loading="this.$store.state.isloading">搜索</el-button>
            <el-button :disabled="this.$store.state.isloading || downloadLoading"  @click="btnRepeat">重置</el-button>
        </div>
        <div>
          <ul class="toolList">
            <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
              <el-button :disabled="$store.state.isloading || downloadLoading" type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
            </li>
            <li>
            <el-button type="primary" :disabled="this.$store.state.isloading" @click="exportExcel">导出Excel</el-button>
            </li>
          </ul>
        </div>
        <dataTable v-if="headListhas" :headList="headList" :dataList="dataList" @selectRow="selectRow" :hiddenColumn="hiddenColumn" :hiddenPageOrder="hiddenPageOrder" :ColumnSortable="ColumnSortable" :height="height"></dataTable>
        <printmodal ref="printPdf"></printmodal>
        <h2 style="text-align:center;">{{message}}</h2>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}
.btn {
  text-align: center;
}
</style>
<script>
import dataTable from "@/components/Table/DataTable";
import printmodal from "@/components/Pdf/Print";
import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import sweetModal from "@/components/sweetModal";

const INFO = "详情";
const EXPORT = "打印";

export default {
  name: "saleStatistics",
  mixins: [manage],
  data() {
    return {
      selectData: {
        options: [],
        RegdateGreaterAndEquals: null,
        RegdateLessAndEquals: null
      },
      downloadLoading:false,
      OutDataExcel:[],
      chargetype: [],
      message: null,
      title: null,
      order: null,
      headerHold: {},
      headerArea: {},
      hiddenColumn: false,
      height:"620",
      hiddenPageOrder: false,
      ColumnSortable: "",
      childrenOption1: [],
      childrenOption2: [],
      household: [],
      area: [],
      headListhas: true,
      options: [
        {
          value: "A",
          label: "乡镇查询",
          children: []
        },
        {
          value: "B",
          label: "户籍查询",
          children: []
        },
        {
          value: "C",
          label: "时间查询",
        },
      ]
    };
  },
  methods: {
    exportExcel() {
      var startTime
      var endTime
      if(this.selectData.RegdateGreaterAndEquals){
        startTime = this.selectData.RegdateGreaterAndEquals.toLocaleDateString();
      }else{
        startTime = '空';
      }
      if(this.selectData.RegdateLessAndEquals){
        endTime = this.selectData.RegdateLessAndEquals.toLocaleDateString();
      }else{
        endTime = '空';
      }
      var header = '火化人员基本情况统计' + '(' + startTime + '--' + endTime + ')';
       if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      var sss={};
      sss = {ADDNAME:'填报单位',FIRENUM:localStorage.orgname};
      // console.log(this.OutDataExcel)
      if(this.OutDataExcel[this.OutDataExcel.length-1].ADDNAME!="填报单位"){
      this.OutDataExcel.push(sss);
      }

      this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel_Merge } = require("@/vendor/Export2Excel");
        export_json_to_excel_Merge(this.headList, this.OutDataExcel, header,undefined,'mergeheader');
        this.downloadLoading = false;
      });
    },
     //功能操作
    operate(order) {
      this.order = order;
      this.dataRepeat();
      switch (this.order) {
        case EXPORT:
          let Data = JSON.parse(JSON.stringify(this.selectData));
          if (Data.RegdateGreaterAndEquals === null || Data.RegdateGreaterAndEquals === undefined || Data.RegdateLessAndEquals===null||Data.RegdateLessAndEquals===undefined){
            this.$message.error("处理日期不能为空");
          }
          else{
              if (Data.options[0] === "A") this.$refs.printPdf.report(PRINT.PDFCREMERADDRESSBYTOWN,this.getDataC());
              else this.$refs.printPdf.report(PRINT.PDFCREMERADDRESSBYHOLD,this.getDataC());
          }
          break;
        default:
      }
    },
    getOptions() {
      this.options[0].children = this.area;
      this.options[1].children = this.household;
    },
    getDataC(){
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.RegdateGreaterAndEquals = formatDate(
        Data.RegdateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.RegdateLessAndEquals = formatDate(
        Data.RegdateLessAndEquals,
        "yyyyMMdd"
      );
      Data.REGADDID = Data.options[Data.options.length - 1];
      Data.REGHOUSEHOLD = Data.options[Data.options.length - 1];      
      if (Data.REGADDID.length === 1) Data.REGADDID = "";
      if (Data.REGHOUSEHOLD.length === 1) Data.REGHOUSEHOLD = "";
      return Data;
    },
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      if (Data.RegdateGreaterAndEquals === null || Data.RegdateGreaterAndEquals === undefined || Data.RegdateLessAndEquals===null||Data.RegdateLessAndEquals===undefined){
        this.$message.error("处理日期不能为空");
      }
      else{
        this.headListhas = false;
        Data.RegdateGreaterAndEquals = formatDate(
        Data.RegdateGreaterAndEquals,
        "yyyyMMdd"
        );
        Data.RegdateLessAndEquals = formatDate(
          Data.RegdateLessAndEquals,
          "yyyyMMdd"
        );
        Data.REGADDID = Data.options[Data.options.length - 1];
        Data.REGHOUSEHOLD = Data.options[Data.options.length - 1];      
        if (Data.REGADDID.length === 1) Data.REGADDID = "";
        if (Data.REGHOUSEHOLD.length === 1) Data.REGHOUSEHOLD = "";
        if (Data.options[0] === "A") {
          apiBase.DoService(
            Data,
            apiBase.getApiAdress().Report.QueryCremerAddressByTown,
            res => {
              this.headListhas = true;
              this.message = res.data.content.Message;
              this.dataList = res.data.content.OutData;
              this.OutDataExcel = res.data.content.OutDataExcel;
              this.OutDataExcel.push({ADDNAME:"备注",UMEM:res.data.content.Message});
              this.headList = [
                {ColumnAlign: "center",ColumnKey: "ADDNAME",ColumnName: "地点名称",ColumnOrder: "ADDNAME"},
                {ColumnAlign: "center",ColumnKey: "FIRENUM",ColumnName: "火化数",ColumnOrder: "FIRENUM"},
                {ColumnAlign: "center",ColumnName: "性別",ColumnKey:"",ColumnChild: [
                    {ColumnAlign: "center",ColumnKey: "MEM",ColumnName: "男",ColumnOrder: "MEM",ColumnWidth: 100},
                    {ColumnAlign: "center",ColumnKey: "UMEM",ColumnName: "女",ColumnOrder: "UMEM",ColumnWidth: 100},
                    {ColumnAlign: "center",ColumnKey: "SEXOTHER",ColumnName: "其他",ColumnOrder: "SEXOTHER",ColumnWidth: 100}]
                },
                {ColumnAlign: "center",ColumnName: "户口",ColumnKey:"",ColumnChild: [
                    {ColumnAlign: "center",ColumnKey: "AGRICULTURE",ColumnName: "农业户口",ColumnOrder: "AGRICULTURE",ColumnWidth: 130},
                    {ColumnAlign: "center",ColumnKey: "NOAGRICULTURE",ColumnName: "非农业户口",ColumnOrder: "NOAGRICULTURE",ColumnWidth: 200},
                    {ColumnAlign: "center",ColumnKey: "OTHERAGRICULTURE",ColumnName: "其他",ColumnOrder: "OTHERAGRICULTURE",ColumnWidth: 130}]
                }];
            }
          );
        } else if(Data.options[0] === "B") {
          apiBase.DoService(
            Data,
            apiBase.getApiAdress().Report.QueryCremerAddressByHold,
            res => {
              this.headListhas = true;
              this.message = res.data.content.Message;
              this.dataList = res.data.content.OutData;
              this.OutDataExcel = res.data.content.OutDataExcel;
              this.headList = [
                {ColumnAlign: "center",ColumnKey: "ADDNAME",ColumnName: "地点名称",ColumnOrder: "ADDNAME"},
                {ColumnAlign: "center",ColumnKey: "FIRENUM",ColumnName: "火化数",ColumnOrder: "FIRENUM"}];
            }
          );
        }else if(Data.options[0] === "C") {
          apiBase.DoService(
            Data,
            apiBase.getApiAdress().Report.QueryCremerAddressByDate,
            res => {
              this.headListhas = true;
              this.message = res.data.content.Message;
              this.dataList = res.data.content.OutData;
              this.OutDataExcel = res.data.content.OutDataExcel;
              this.headList = [
                {ColumnAlign: "center",ColumnKey: "REGCREMATIONDATEDESC",ColumnName: "火化日期",ColumnOrder: "REGCREMATIONDATEDESC"},
                {ColumnAlign: "center",ColumnKey: "FIRENUM",ColumnName: "火化数",ColumnOrder: "FIRENUM"}];
            }
          );
        }
      }
    },
    //获取户籍所在地
    getHousehold() {
      let data = {};
      apiBase.DoService(data, apiBase.getApiAdress().Code.GetHousehold, res => {
        let cardata = res.data.content;
        this.household = cardata.map(item => ({
          label: item.StaName,
          value: item.StaCode,
          children: item.List_Cty.map(itemCty => ({
            label: itemCty.CtyName,
            value: itemCty.CtyCode,
            children: itemCty.List_Cnt.map(itemCnt => ({
              label: itemCnt.CntName,
              value: itemCnt.CntCode
            }))
          }))
        }));
      });
    },
    getAddr() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Code.GetAddressMap,
        res => {
          let cardata = res.data.content;
          this.area = cardata.map(item => ({
            value: item.ADDID,
            label: item.ADDNAME,
            ADDLEVEL: item.ADDLEVEL,
            ADDTOPID: item.ADDTOPID,
            children: item.Children.map(itemStr => ({
              value: itemStr.ADDID,
              label: itemStr.ADDNAME,
              ADDLEVEL: itemStr.ADDLEVEL,
              ADDTOPID: itemStr.ADDTOPID
            }))
          }));
        }
      );
    },
    //查询条件重置
    btnRepeat() {
      this.selectData = {};
      this.selectData.options = ["A"];
      this.selectData.RegdateGreaterAndEquals = new Date();
      this.selectData.RegdateGreaterAndEquals.setDate(1);
      var day = new Date();
      var year = day.getFullYear();
      var month = day.getMonth()+1;
      var d = new Date(year, month, 0);
      var dat = d.getDate();
      this.selectData.RegdateLessAndEquals = new Date();
      this.selectData.RegdateLessAndEquals.setDate(dat);
      this.getData();
    },
    btnShow(btn){
      switch (btn) {
        case EXPORT:
          return true;
          break;
          // case EXPORT:
          //   let Data = JSON.parse(JSON.stringify(this.selectData));
          //   if (Data.options[0] === "A") this.$refs.printPdf.report(PRINT.PDFSALESTATISTICSBYCYP,this.getDataC());
          //   else this.$refs.printPdf.report(PRINT.PDFSALESTATISTICS,this.getDataC());
          //   break;
        default: return false;
        }
    },
    //数据重置
    dataRepeat() {}
  },
  mounted() {},
  components: {
    dataTable,
    printmodal
  },
  //当前选中行
  selectRow(row) {
    this.currentRow = row;
    this.$emit("selectRow", row, "");
  },
  created() {
    this.selectData.RegdateGreaterAndEquals = new Date();
    this.selectData.RegdateGreaterAndEquals.setDate(1);
    this.selectData.RegdateLessAndEquals = new Date();
    var day = new Date();
    var year = day.getFullYear();
    var month = day.getMonth()+1;
    var d = new Date(year, month, 0);
    var dat = d.getDate();
    this.selectData.RegdateLessAndEquals.setDate(dat);
    this.selectData.options = ["A"];
    this.getHousehold();
    this.getAddr();
    this.getData();
    var t;
    clearTimeout(t);
    t = setTimeout(() => {
      this.getOptions();
    }, 2000);
  }
};
</script>